/*
 * Copyright 2010 Florian Sauter
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.rentoudu.chat.server.servlet;

import java.io.IOException;
import javax.servlet.http.*;

import com.google.appengine.api.channel.ChannelService;
import com.google.inject.Inject;

import de.rentoudu.chat.server.guice.ChannelKey;

/**
 * Creates a token for a client. Should be used carefully! Costs a lot of CPU.
 * 
 * @author Florian Sauter
 */
public class ChannelTokenServlet extends HttpServlet {
	
	private static final long serialVersionUID = 521089702903278697L;

	private final ChannelService channelService;
	private final String channelKey;
	
	@Inject
	public ChannelTokenServlet(final ChannelService channelService, final @ChannelKey String channelKey) {
		this.channelService = channelService;
		this.channelKey = channelKey;
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		String token = channelService.createChannel(channelKey);
		resp.getWriter().print(token);
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doGet(req, resp);
	}
}